# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
#include ActionView::Helper::JavaScriptHelper
  # Return a link for use in site navigation.
  def nav_link(text, controller, action="index")
    link_to_unless_current text, :id => nil,
                                 :action => action,
                                 :controller => controller
  end

  def to_cn(str)
    #str.gsub!('about', '大约').gsub!('days','天之前').gsub!('hours','小时之前') .gsub!('minutes','分钟之前')
    str.gsub(/day[s]?/,'天之前').gsub(/hour[s]?/, '小时之前').gsub(/minute[s]?/, '分钟之前').gsub(/about[\s]?/, '大约').gsub(/less than a[\s]?/, '少于1')
    #return str += "之前"
  end

  def get_current_tags(cloud=true)
    tags = current_user.bookmarks.tag_counts
    if cloud
      add_linked_tags(tags)
    else
      add_function_tags(tags)
    end
  end

  def get_all_tags(limit=50)
    tags = Bookmark.tag_counts :limit=>limit
    add_linked_tags(tags)
  end

  def add_linked_tags(tags)
    taglist = ""
    tag_cloud tags, %w(nube1 nube2 nube3 nube4 nube5) do |tag, css_class|
      taglist = taglist + "<a href='/tag/#{tag}' class=#{css_class}>#{tag}</a> "
    end
    taglist
  end

  #TODO: add link function tag for select
  def add_function_tags(tags)
    taglist = []
#    tag_cloud tags, %w(nube1 nube2 nube3 nube4 nube5) do |tag, css_class|
#      taglist += "<a href='/tag/#{tag}' class=#{css_class}>#{tag}</a> "
#    end
    tags.map{|tag|
      taglist << link_to_function(tag.name, "$('bookmark_tag_list').value+='"+tag.name+"';return false;")
        ##"<a href='javascript:selecttag(\""+tag.name+"\")'>"+tag.name+"</a>"
    }
    #"<script>function selecttag(t){$(\"bookmark_tag_list\").value+=t;return false;}</script>" + taglist.join(" ")
    taglist.join(" ")
  end
#  def tag_cloud(tags, classes)
#    max, min = 0, 0
#    tags.each { |t|
#      max = t.count.to_i if t.count.to_i > max
#      min = t.count.to_i if t.count.to_i < min
#    }
#
#    divisor = ((max - min) / classes.size) + 1
#
#    tags.each { |t|
#      yield t.name, classes[(t.count.to_i - min) / divisor]
#    }
#  end

  def add_tag_link(taglist)
    list = ""
    taglist.split(%r{ \s*}).each {|name|
    list += link_to( name, "/tag/"+name) + " "
    }
    list
  end


  def add_tag_link_function(taglist)
    list = ""
    taglist.split(%r{ \s*}).each {|name|
    list += link_to_function( name, "$('tags').value+=' "+name+"';return false;") + " "
    }
    list
  end

  
end
